package game.editer.ui;

import haxe.Timer;
import openfl.Lib;
import motion.Actuate;
import hx.events.Keyboard;
import hx.events.Event;
import hx.events.KeyboardEvent;
import hx.events.MouseEvent;
import hx.display.HBox;
import hx.display.Label;
import hx.display.Quad;
import hx.display.Box;

/**
 * 按键指令输入组件
 */
class KeyInput extends BaseAttributeInput {
	public var keys:HBox;

	public var currentKey:KeyDisplay;

	override function onInit() {
		super.onInit();
		keys = new HBox();
		this.addChild(keys);
		keys.gap = 3;
		keys.addEventListener(MouseEvent.CLICK, onReadyInput);
		this.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
		this.addEventListener(Event.FOCUS_OVER, onFocusOver);
		this.addEventListener(Event.FOCUS_OUT, onFocusOut);
	}

	private function onFocusOver(e:Event):Void {}

	private function onFocusOut(e:Event):Void {
		for (object in keys.children) {
			var key2:KeyDisplay = cast(object, KeyDisplay);
			key2.setSelected(false);
		}
		currentKey = null;
	}

	private function onKeyUp(e:KeyboardEvent):Void {
		if (currentKey != null) {
			currentKey.keyCode = e.keyCode;
			this.value = this.value;
			var index = currentKey.index + 1;
			currentKey = cast this.keys.children[index];
			currentKey.setSelected(true);
			Timer.delay(() -> {
				this.stage.focus = currentKey;
			}, 16);
		}
	}

	private function onReadyInput(e:MouseEvent):Void {
		if (e.target is KeyDisplay) {
			var key = cast(e.target, KeyDisplay);
			currentKey = key;
			for (object in keys.children) {
				var key2:KeyDisplay = cast(object, KeyDisplay);
				key2.setSelected(object == key);
			}
			key.setSelected(true);
		}
	}

	override function set_value(value:Dynamic):Dynamic {
		if (value == null) {
			value = [];
		}
		var array:Array<Int> = value;
		keys.removeChildren();
		for (index => i in array) {
			var k = new KeyDisplay(i);
			k.index = index;
			keys.addChild(k);
		}
		var k = new KeyDisplay("+".charCodeAt(0), 0x3e3e3e);
		k.index = array.length;
		keys.addChild(k);
		return super.set_value(value);
	}

	override function get_value():Dynamic {
		var array = [];
		for (object in keys.children) {
			var key = cast(object, KeyDisplay);
			if (key.isHasKey()) {
				array.push(key.keyCode);
			}
		}
		return array;
	}
}

class KeyDisplay extends Box {
	/**
	 * 键控值
	 */
	public var keyCode(get, set):Int;

	private var __keyCode:Int = -1;

	private function get_keyCode():Int {
		return __keyCode;
	}

	private function set_keyCode(value:Int):Int {
		this.__keyCode = value;
		if (label != null)
			label.data = String.fromCharCode(value);
		return value;
	}

	public var color:UInt;

	public var quad:Quad;

	public var label:Label;

	public var index:Int = -1;

	public var selected = false;

	public function isHasKey():Bool {
		if (__keyCode == Keyboard.BACKSPACE || __keyCode == Keyboard.ESCAPE) {
			return false;
		}
		return __keyCode != "+".charCodeAt(0);
	}

	public function new(key:Int, color:UInt = 0xcccccc) {
		this.keyCode = key;
		this.color = color;
		super();
		this.mouseChildren = false;
	}

	override function onInit() {
		super.onInit();
		this.width = this.height = 20;
		quad = new Quad(this.width, this.height, color);
		this.addChild(quad);

		label = new Label(String.fromCharCode(this.keyCode), this.color == 0xcccccc ? Config.KEY_TEXT_FORMAT : Config.KEY_TEXT_ADD_FORMAT);
		label.width = label.height = 20;
		label.horizontalAlign = CENTER;
		this.addChild(label);
	}

	public function setSelected(selected:Bool):Void {
		quad.data = selected ? 0x956457 : color;
		this.selected = selected;
	}
}
